package com.data.biz.utils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import java.io.StringWriter;
import java.text.SimpleDateFormat;

public class JSONUtilsEx {
    private static ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.setDateFormat(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"));
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public JSONUtilsEx() {
    }

    public static String serialize(Object obj) {
        StringWriter writer = null;

        try {
            writer = new StringWriter();
            mapper.writeValue(writer, obj);
            writer.close();
        } catch (Exception var3) {
            throw new ServiceException("JSON序列化结果异常:" + var3.getMessage());
        }

        return writer.toString();
    }

    public static <T> T deserialize(String jsonStr, Class<T> clazz) {
        if (StringUtils.isBlank(jsonStr)) {
            return null;
        } else {
            try {
                return mapper.readValue(jsonStr.replace("\n", ""), clazz);
            } catch (Exception var3) {
                throw new ServiceException("JSON反序列化结果异常:" + var3.getMessage());
            }
        }
    }
}
